//
//  PhoneListViewController.m
//  tangshantongjiAPP
//
//  Created by yuntai on 2018/4/23.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "PhoneListViewController.h"
#import "PhoneListCell.h"
#import "FullMainModel.h"
#import "AlertViewManager.h"

@interface PhoneListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)NSMutableArray *array;
@property (weak, nonatomic) IBOutlet UITextField *searchTF;
@property (nonatomic, copy) NSString *searchStr;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation PhoneListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.isFirst = YES;
    self.searchStr = @"";
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight+45, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight-45) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 170;
    tableview.sectionHeaderHeight = 30;
    tableview.tableFooterView = [[UIView alloc]init];
    [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];
    
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
    
}

- (IBAction)searchAction:(UIButton *)sender {
    [self.view endEditing:YES];
    
    NSString *searString = [self.searchTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (searString.length == 0 && self.isFirst == YES) {
        [HUDManager showWarningWithText:@"请输入要搜索的内容"];
        return;
    }
    self.isFirst = NO;
    self.searchStr = searString;
    self.currentPage = 1;
    [self.tableview.mj_header beginRefreshing];
}
#pragma mark tableviewdelegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pricecellid = @"PhoneListCell";
    PhoneListCell *cell = [tableView dequeueReusableCellWithIdentifier:pricecellid];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"PhoneListCell" owner:nil options:0]firstObject];
    }
    FullMainModel *model = self.array[indexPath.section];
    cell.zhuanyemingcLabel.text = model.name?:@"";
    cell.yewufanweiLabel.text = model.service?:@"";
    [cell.telPhoneBtn setTitle:model.telephone?:@"" forState:UIControlStateNormal];
    [cell.phoneBtn setTitle:model.mobile?:@"" forState:UIControlStateNormal];
    cell.chargeLabel.text = model.user_name?:@"";
    cell.btnBlock = ^(NSInteger index, NSString *num) {
        
        [AlertViewManager showAlertViewMessage:[@"确定要拨打电话:" stringByAppendingString:num?:@""] handlerBlock:^{
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[@"tel://" stringByAppendingString:num]]];

        }];
        
    };
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}

- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:self.searchStr forKey:@"name"];
    [params setValue:@(self.currentPage) forKey:@"page"];
    
    [NetWork PostNetWorkWithUrl:@"api/phone/getAll.ajax" with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = dic[@"data"][@"data"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[FullMainModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    }];
    
}
@end
